﻿<?php
class QSale
{	
	public function __construct($p_strCountry)
	{
		$this->_country	= $p_strCountry;
		$this->_table	= strtolower($p_strCountry).'_sales';
		
		$this->_path_top_sku	= ROOT_SITE.'/data/top_sku';
		@mkdir($this->_path_top_sku);
		
		$this->_db		= new Common_Db('wms_'.$this->_country);
		$this->_dbbob		= new Common_Db('wms_'.$this->_country . '_bob');
	}
	
	function __destruct() 
	{
		$this->_db->closeConnection();
		$this->_dbbob->closeConnection();
	}
		
	public function save_top_sku($p_strType, $p_arrResult)
	{
		$file	= $this->_path_top_sku.'/'.$this->_country.'.'.$p_strType.'.data';
		file_put_contents($file, json_encode($p_arrResult));
	}
	
	public function get_top_sku($p_strType)
	{
		$file		= $this->_path_top_sku.'/'.$this->_country.'.'.$p_strType.'.data';
		$strContent	= @file_get_contents($file);
		return $strContent ? json_decode($strContent, true) : null;
	}
	
	public function get($p_intOffset, $p_intLimit)
	{
		$strQuery	= '
			SELECT * 
			FROM '.$this->_table.'
			ORDER BY sku ASC';
		if($p_intLimit!='all'){
			$strQuery	= $strQuery.' LIMIT '.$p_intOffset.','.$p_intLimit;
		}
		$db 	= Zend_Registry::get('db');
		$resultValue = $db->fetchAll($strQuery);
		$db->closeConnection();
		return $resultValue;
	}

	public function get_cal()
	{
		$strQuery	= '
			SELECT sku, price_paid_by_customer `price`
			FROM '.$this->_table.'
			ORDER BY sku ASC';
		$db 	= Zend_Registry::get('db');
		$resultValue = $db->fetchAll($strQuery);
		$db->closeConnection();
		return $resultValue;
	}
	
	public function total_revenue()
	{
		$strQuery	= '
			SELECT SUM(price_paid_by_customer*1) `total`
			FROM '.$this->_table.'
			';
		$db 		= Zend_Registry::get('db');
		$arrResult 	= $db->fetchAll($strQuery);
		$db->closeConnection();
		return isset($arrResult[0]['total']) ? $arrResult[0]['total'] : 0;
	}
	
	public function get_category()
	{
		$strQuery	= 'SELECT DISTINCT(category_level_1) `name` FROM '.$this->_table;
		$db 		= Zend_Registry::get('db');
		$resultValue = $db->fetchAll($strQuery);
		$db->closeConnection();
		return $resultValue;
	}
	
	public function get_total_category($p_strCategory)
	{
		$strQuery	= '
			SELECT SUM(supplier_lead_time) `total` 
			FROM '.$this->_table.' 
			WHERE LOWER(category_level_1)="'.strtolower($p_strCategory).'"
		';
		$db 		= Zend_Registry::get('db');
		$arrResult	= $db->fetchAll($strQuery);
		$db->closeConnection();
		return isset($arrResult[0]['total']) ? $arrResult[0]['total'] : 0;
	}
	
	public function total()
	{
		$strQuery	= '
			SELECT COUNT(id) `total`
			FROM '.$this->_table;
		$db 		= Zend_Registry::get('db');
		$arrResult 	= $db->fetchAll($strQuery);
		$db->closeConnection();
		return isset($arrResult[0]['total']) ? $arrResult[0]['total'] : 0;
	}
	
	public function get_by_id($p_intId)
	{
		$strQuery		= 'SELECT * FROM '.$this->_table.' WHERE id='.intval($p_intId);
		$db 			= Zend_Registry::get('db');
		$resultValue	= $db->fetchAll($strQuery);
		$db->closeConnection();
		return $resultValue;
	}
	
	public function get_top_by_sku($p_sku)
	{
		$strQuery	= "SELECT * FROM ".$this->_table." WHERE sku='". $p_sku ."' LIMIT 0, 1;";
		
		$db 		= Zend_Registry::get('db');
		$resultValue= $db->fetchAll($strQuery);
		$db->closeConnection();
		
		return $resultValue;
	}
	
	public function insert($p_arrParams)
	{
		$db	= Zend_Registry::get('db');
		$resultValue = $db->insert($this->_table, $p_arrParams);
		$db->closeConnection();
		return $resultValue;
	}
	
	public function update($p_arrParams, $p_intId)
	{
		$db	= Zend_Registry::get('db');
		$resultValue = $db->update($this->_table, $p_arrParams, 'id='.intval($p_intId));
		$db->closeConnection();
		return $resultValue;
	}
	
	public function reset()
	{
		$strQuery	= 'TRUNCATE '.$this->_table;
		$db 		= Zend_Registry::get('db');
		$db->execute($strQuery);
		$db->closeConnection();
	}
	
	public function get_item_analytic($p_strSku, $p_strDateFrom, $p_strDateTo, $p_arrParams=array())
	{
		$strQuery	= '
			SELECT sku, DATE_FORMAT(date, "%Y-%m-%d") `date`,
			 LOWER(item_level_status) `istatus`
			FROM '.$this->_table.'
			WHERE sku IN ("'.$p_strSku.'") and (`item_level_status` is NULL OR `item_level_status` ="")
		';
		
		if($p_strDateFrom){
			$strQuery	= $strQuery.' AND date>="'.$p_strDateFrom.' 00:00:00"';
		}
		if($p_strDateTo){
			$strQuery	= $strQuery.' AND date<="'.$p_strDateTo.' 23:59:59"';
		}
		if( isset($p_arrParams['orf']) && $p_arrParams['orf'] ){
			$strQuery	= $strQuery.' ORDER BY '.$p_arrParams['orf'].' '.$p_arrParams['orv'];		
		}
		else{
			$strQuery	= $strQuery.' ORDER BY sku ASC';		
		}
		
		$db 		= Zend_Registry::get('db');
		$resultValue = $db->fetchAll($strQuery);		
		$db->closeConnection();
		return $resultValue;
	}
	
	public function get_item_for_calculation_cancellation_rate($p_strSku, $p_strDateFrom, $p_strDateTo, $p_arrParams=array())
	{
		$strQuery	= '
			SELECT sku, DATE_FORMAT(date, "%Y-%m-%d") `date`,
			 LOWER(item_level_status) `istatus`
			FROM '.$this->_table.'
			WHERE sku IN ("'.$p_strSku.'") 
		';
		
		if($p_strDateFrom){
			$strQuery	= $strQuery.' AND date>="'.$p_strDateFrom.' 00:00:00"';
		}
		if($p_strDateTo){
			$strQuery	= $strQuery.' AND date<="'.$p_strDateTo.' 23:59:59"';
		}
		if( isset($p_arrParams['orf']) && $p_arrParams['orf'] ){
			$strQuery	= $strQuery.' ORDER BY '.$p_arrParams['orf'].' '.$p_arrParams['orv'];		
		}
		else{
			$strQuery	= $strQuery.' ORDER BY sku ASC';		
		}
		
		$db 		= Zend_Registry::get('db');
		$resultValue = $db->fetchAll($strQuery);		
		$db->closeConnection();
		return $resultValue;
	}
	
	public function get_max_date($p_bolFull=false)
	{
		$strQuery	= '
			SELECT 
				DATE_FORMAT(max(date), "%Y-%m-%d 23:59:59") `date`, min(date) `mdate` 
			FROM '.$this->_table;
			
		$db 		= Zend_Registry::get('db');
		$arrResult	= $db->fetchAll($strQuery);
		$db->closeConnection();
		
		if($p_bolFull){
			return isset($arrResult[0]) ? $arrResult[0] : null;
		}
		return isset($arrResult[0]['date']) ? $arrResult[0]['date'] : null;
	}
	
	public function getSaleDataFromOMS()
	{
		//Update Category 
		$objSupplier = new QSupplier($this->_country);
		$arrSupplier = $objSupplier->getWeekday();
		$db	= Zend_Registry::get('db');
		
		$strQuery	= 'SELECT 	i.created_at as date,
						i.sku,
						i.paid_price as price_paid_by_customer,
						i.name, 
						m.fk_Sales_order_item_status as item_level_status
						FROM ims_sales_order_item i 
						inner join ims_sales_order_item_status k on 
						i.fk_sales_order_item_status = k.id_sales_order_item_status
						inner join ims_sales_order l on
						i.fk_sales_order = l.id_Sales_order 
						left outer join oms_live.ims_sales_order_item_status_history m on
						i.id_Sales_order_item = m.fk_sales_order_item
						and m.fk_Sales_order_item_status = "9" 
						where i.created_at >= Date_Sub(sysdate(),interval 30 day);';
		$arrData	= $this->_db->fetchAll($strQuery);
		//Combine become name of image.
		if($arrData)
		{
			$this->reset();
			foreach ($arrData as $data)
			{	
				$p_arrParams =array(
					'sku' => $data['sku'],
					'date' => $data['date'],
					'product_name' => $data['name'],
					'category_level_1' => '',
					'price_on_website' => '0',
					'price_paid_by_customer' => $data['price_paid_by_customer'],
					'item_level_status' => $data['item_level_status']=='9'?'cancelled':NULL,
					'order_level_status' => '',
					'supplier_lead_time' => '',
					'shipment_type' => '',
					);
				$resultValue = $db->insert($this->_table, $p_arrParams);
			}
		}
	
		$strIdSupplier ="" ; 
		foreach($arrSupplier as $item)
		{
		  	$strIdSupplier =$strIdSupplier . $item['id'] . ',';
		}
		$strIdSupplier = substr($strIdSupplier, 0 , -1);
		
		$arrDataForecasts =array();
		if($strIdSupplier)
		{			
			$strQuery	= 'Select distinct sku, id_catalog_config, product_name, categoryname, supplier_id  From
								(
								SELECT cc.id_catalog_config, cs.sku `sku`,cc.name as product_name, cat.`name_en` categoryname, 
										cc.fk_catalog_supplier as supplier_id
										FROM catalog_simple cs
										JOIN catalog_config cc ON cc.id_catalog_config = cs.fk_catalog_config
										JOIN catalog_config_has_catalog_category cccat ON cccat.fk_catalog_config = cc.id_catalog_config
										JOIN catalog_category cat ON cccat.fk_catalog_category = cat.id_catalog_category
									   WHERE cc.status = "active"
										AND cat.id_catalog_category <> 1
										AND cc.status_supplier_config = "active"
										 AND cat.status = "active"
										 AND cc.pet_approved = 1
										 AND EXISTS (SELECT 1
										   FROM catalog_brand cb
										  WHERE cb.id_catalog_brand = cc.fk_catalog_brand
											AND cb.status = "active")
										 AND EXISTS (SELECT 1
										   FROM catalog_supplier sup
										  WHERE sup.id_catalog_supplier = cc.fk_catalog_supplier
											AND sup.status = "active")
										 AND EXISTS (SELECT 1
										   FROM catalog_category cat
										   JOIN catalog_config_has_catalog_category cccat ON cccat.fk_catalog_category = cat.id_catalog_category
										  WHERE cccat.fk_catalog_config = cc.id_catalog_config
											AND cat.status = "active"
											AND cat.id_catalog_category <> 1)
										 AND cs.status = "active"
										 AND cs.status_supplier_simple = "active"
										 AND cs.price > 0
										 AND (cc.display_if_out_of_stock = 1
										OR
										(IFNULL((SELECT CAST(st.quantity AS SIGNED INT)
											 FROM catalog_stock st
											WHERE st.fk_catalog_simple = cs.id_catalog_simple), 0)
										 -
										 (SELECT COUNT(1)
										 FROM sales_order_item soi
										   WHERE soi.sku = cs.sku
										  AND soi.is_reserved = 1)) > 0)
									   Order by cs.sku , cat.lft ASC
								) tmp 
								group by tmp.sku;';
			$arrDataForecasts	= $this->_dbbob->fetchAll($strQuery);
		}
	
		if($arrDataForecasts)
		{
			$objForecasts = new QBForecast($this->_country);
			$objForecasts->reset();
					    
			foreach($arrDataForecasts as $data)
			{	
				$p_arrParams =array(
					'sku' => $data['sku'],
					'product_name' =>  $data['product_name'],
					'category' =>  $data['categoryname'],
					'supplier_id' => $data['supplier_id'],
					'total_sales' => '0'
					);  
				$db->insert($this->_country ."_forecasts", $p_arrParams);
			} 
			// Update category with category mapping
			$strQuery ="UPDATE " . $this->_country . "_forecasts f
						INNER JOIN category_mapping cat ON cat.level2 = f.category 
														OR cat.level3 = f.category 
														OR cat.level4 = f.category 
														OR cat.level5 = f.category  
														OR cat.level6 = f.category  
						SET f.category = cat.level1;";
			// $db->execute($strQuery);
				
			//Update sales cateogry 
			$strQuery ="Update " . $this->_table . " s Left join " . $this->_country . "_forecasts f ON f.sku = s.sku 
						SET s.category_level_1 = case when f.category IS NULL then 'Invisible' else f.category END;";
			$db->execute($strQuery);
			
			//Update Leadtime for sales table
			$strQuery ="Update " . $this->_table . " s Inner join " . $this->_country . "_forecasts f ON f.sku = s.sku ". 
				"Inner join " . $this->_country . "_suppliers sup on f.supplier_id = sup.id " . 
				" SET s.supplier_lead_time = sup.leadtime, s.supplier_order_frequency = sup.order_frequency; ";
			$db->execute($strQuery);
			$db->closeConnection();
		}
		
		//Backup table PoHeader and PoDetail
		$now = date('YmdHis');
		$objPOHeader = new QPoHeader($this->_country);
		$objPOHeader->insert_Archives(); 
		$objPOHeader->truncate();
		
		$objPODetail = new QPoDetail($this->_country);
		$objPODetail->insert_Archives(); 
		$objPODetail->truncate();
		
		//Insert data to POHeader
		$strQuery = 'SELECT distinct supplier_id FROM '. $this->_country . '_forecasts';
		$arrDateSupplier	= $db->fetchAll($strQuery);
		
		if($arrDateSupplier)
		{
			$i = 1;
			$weedday = CConfig::getCurrentDay();
			$daymonth = CConfig::getCurrentDayMonthYear();
			
			foreach($arrDateSupplier as $item)
			{
				if($item['supplier_id'])
				{
					$arrInsert = array(
						'supplier_id' =>  $item['supplier_id'],
						'po_number'	  =>  $weedday . $daymonth . substr('0000' .  (string) $i, -4)	
						);	
					$objPOHeader->insert($arrInsert);
					$i = $i+ 1 ;
				}
			}
		}
		
		//Update Shipment type for sales table
		$strQuery	= 'SELECT i.sku, j.name_en as shipment_type FROM bob_live.catalog_simple i 
						Inner join bob_live.catalog_shipment_type j on
								i.fk_catalog_shipment_type = j.id_catalog_shipment_type;';
		$arrDataShipment	= $this->_dbbob->fetchAll($strQuery);
	
		if($arrDataShipment)
		{			
			foreach($arrDataShipment as $data)
			{
				$p_arrParams =array(
					'shipment_type' => $data['shipment_type']=='Own Warehouse'?'c':'o'  //Own Warehouse : C / Cross-Docking : O / Dropshipping : O
					);
				
				$db->update($this->_table, $p_arrParams, 'sku="'.$data['sku'] .'"');
			}
		}
		
		$db->closeConnection();	
		
		# Call caculateForcasts()
		$this->caculateForcasts();
				
		#Save Parameter 
		
		$this->saveParameterAuto();
	}
	
	public function caculateForcasts()
	{
		# Save category from sales data
		$arrData				= array();
		$arrData['category']	= $this->get_category();
		$objCategory	= new CCategory($this->_country);
		$objCategory->save($arrData);
		
		$this->_cal_top_sku();
		CCommon::clear_chart_cache($this->_country);
		
		#Bucket
		$arrTopItem		= $this->get_top_sku('items');
		$arrTopRevenue	= $this->get_top_sku('revenue');
		
		$strQuery	= '
					UPDATE '. $this->_country .'_forecasts SET _bucket="long_tail"  , bucket_id = 4 
					WHERE _bucket IS NULL OR _bucket="" ';
		$db	= Zend_Registry::get('db');
		$db->execute($strQuery);
		if($arrTopItem)
		{
			$strQuery	= '
					UPDATE '.$this->_country.'_forecasts SET _bucket="volume_driver" , bucket_id = 2 
					WHERE LOWER(sku) IN ("'.strtolower(implode('","', $arrTopItem)).'")';
			$db->execute($strQuery);
		}
		if($arrTopRevenue)
		{
			$strQuery	= '
					UPDATE '.$this->_country.'_forecasts SET _bucket="revenue_driver" , bucket_id = 3 
					WHERE LOWER(sku) IN ("'.strtolower(implode('","', $arrTopRevenue)).'")';
			$db->execute($strQuery);
		}
		if($arrTopItem && $arrTopRevenue)
		{
			$strQuery	= '
					UPDATE '.$this->_country.'_forecasts SET _bucket="cash_cow", bucket_id = 1  
					WHERE LOWER(sku) IN ("'.strtolower(implode('","', $arrTopItem)).'") AND LOWER(sku) IN ("'.implode('","', $arrTopRevenue).'"); ';
			$db->execute($strQuery);
		}
		#Bucket
		
		#WMS
		$obj		= new QBForecast($this->_country);
		$arrData	= $obj->get_all();
		CrontabController::update_WMS($arrData, $this->_country);
		#WMS
		
		CCommon::clear_chart_cache($this->_country);
		
		#Cancellation Rate
		$this->_cancellation_rate($arrData);
		#Cancellation Rate
		
		$db->closeConnection();
	}
	
	public function _cancellation_rate($p_arrForecastData)
	{
		$arrSku	= array();
		foreach($p_arrForecastData as $data){
			$arrSku[]	= $data['sku'];
		}
		
		$arrParams	= array(
			'orf'	=> 'sku',
			'orv'	=> 'asc, date asc'
		);
		$arrData	= $this->get_item_for_calculation_cancellation_rate(implode('","', $arrSku), null, null, $arrParams);
		
		if($arrData)
		{
			$intConfig		= 15;
			$arrDateConf	= $this->get_max_date(true);
			$start_time		= CCommon::mkTimeDateFrom($arrDateConf['mdate']);
			//$end_time		= CCommon::mkTimeDateFrom($arrDateConf['date'])-(60*60*24*$intConfig);
			$end_time		= CCommon::mkTimeDateFrom($arrDateConf['mdate']) + (60*60*24*$intConfig);
			
			$arrDataSku		= array();
			foreach($arrData as $data)
			{
				if( !isset($arrDataSku[$data['sku']]) ){
					$arrDataSku[$data['sku']]	= array();
				}
				$arrDataSku[$data['sku']][]	= $data;
			}
			
			$arrExclude		= array('cancelled');
			$strQuery		= 'UPDATE '.$this->_country.'_forecasts SET _cancellation_rate=(CASE sku ';
			foreach($arrDataSku as $sku=>$data)
			{
				$intTotal	= 0;
				$intCancel	= 0;
				
				foreach($data as $item)
				{
					$time	= CCommon::mkTimeDate($item['date']);
					if( $time>=$start_time && $time<=$end_time)
					{
						if( in_array(trim($item['istatus']), $arrExclude) ){
							$intCancel++;
						}
						$intTotal++;
					}
				}
				
				$cancellation_rate	= $intTotal>0 ? $intCancel/$intTotal : 0;
				$strQuery	= $strQuery .' WHEN "'.$sku.'" THEN "'.$cancellation_rate.'" ';
			}
		
			$strQuery	= $strQuery.' END) ';
			$strQuery	= $strQuery.' WHERE sku IN ("'.implode('","', $arrSku).'") ';
			
			$db			= Zend_Registry::get('db');
			$db->execute($strQuery);
			$db->closeConnection();
		}
	}
	
	public function _cal_top_sku()
	{
		$arrData	= $this->get_cal();
		
		if($arrData)
		{
			$mixTotalRevenue	= 0;
			$arrSkuItem			= array();
			$arrSkuRevenue		= array();
			$arrSku				= array();
			
			$intTotal			= count($arrData);
			
			for($i=0; $i<$intTotal; $i++)
			{
				if( !in_array($arrData[$i]['sku'], $arrSku) )
				{
					$arrItem	= array(
						'sku'	=> $arrData[$i]['sku'],
						'total'	=> 0
						);
					$arrRevenue	= array(
						'sku'	=> $arrData[$i]['sku'],
						'total'	=> 0
						);
					for($j=$i; $j<$intTotal; $j++)
					{
						if($arrData[$j]['sku']!=$arrData[$i]['sku']){
							$i	= $j-1;
							break;
						}
						$arrItem['total']		+= 1;
						$arrRevenue['total']	+= $arrData[$j]['price'];
						$mixTotalRevenue		+= $arrData[$j]['price'];
					}
					$arrSkuItem[]		= $arrItem;
					$arrSkuRevenue[]	= $arrRevenue;
					$arrSku[]			= $arrData[$i]['sku'];
				}
			}
			
			#Sort
			$intN	= count($arrSku);
			for($i=0; $i<$intN; $i++)
			{
				for($j=$i; $j<$intN; $j++)
				{
					if($arrSkuItem[$j]['total'] > $arrSkuItem[$i]['total'])
					{
						$arrTemp			= $arrSkuItem[$j];
						$arrSkuItem[$j]		= $arrSkuItem[$i];
						$arrSkuItem[$i]		= $arrTemp;
					}
					if($arrSkuRevenue[$j]['total']>$arrSkuRevenue[$i]['total'])
					{
						$arrTemp			= $arrSkuRevenue[$j];
						$arrSkuRevenue[$j]	= $arrSkuRevenue[$i];
						$arrSkuRevenue[$i]	= $arrTemp;
					}
				}
			}
			#Sort
			
			$arrResult_Item		= array();
			$arrResult_Revenue	= array();
			$intStop_Item		= 0;
			$intStop_Revenue	= 0;
			for($i=0; $i<$intN; $i++)
			{
				$mixPreviousItem	= isset($arrSkuItem[$i-1]) ? $arrSkuItem[$i-1]['per'] : 0;
				$mixPreviousRevenue	= isset($arrSkuRevenue[$i-1]) ? $arrSkuRevenue[$i-1]['per'] : 0;
				
				$perSku		= $mixPreviousItem + (($arrSkuItem[$i]['total']/$intTotal)*100);
				$perRevenue	= $mixPreviousRevenue + (($arrSkuRevenue[$i]['total']/$mixTotalRevenue)*100);
				
				$arrSkuItem[$i]['per']		= $perSku;
				$arrSkuRevenue[$i]['per']	= $perRevenue;
				
				if($intStop_Item==0 && $perSku<=80){
					$arrResult_Item[]	= $arrSkuItem[$i]['sku'];
				}
				elseif($perSku>80){
					$intStop_Item	= 1;
				}
				
				if($intStop_Revenue==0 && $perRevenue<=80){
					$arrResult_Revenue[]	= $arrSkuRevenue[$i]['sku'];
				}
				elseif($perRevenue>80){
					$intStop_Revenue	= 1;
				}
				
				if($intStop_Item==1 && $intStop_Revenue==1){
					break;
				}
			}
			$this->save_top_sku('items', $arrResult_Item);
			$this->save_top_sku('revenue', $arrResult_Revenue);
		}
	}
	
	public function saveParameterAuto()
	{
		$strQuery = "Select DISTINCT category as category FROM ". $this->_country . "_forecasts;";
			$db			= Zend_Registry::get('db');
			$arrCate = $db->fetchAll($strQuery);
			
			$strQuery = "Select date FROM ". $this->_country . "_sales Order by date asc Limit 0, 1 ;";
			$arrMinDate = $db->fetchAll($strQuery);
		
			$strQuery = "Select date FROM ". $this->_country . "_sales Order by date desc Limit 0, 1 ;";
			$arrMaxDate = $db->fetchAll($strQuery);
			 
			$db->closeConnection();
		
			if($arrCate)
			{
			foreach($arrCate as $item)
				{
					$strCategory		= $item['category'];
					$strSaleDateFrom	= isset($arrMinDate) ? $arrMinDate[0]['date'] : null;
					$strSaleDateTo		= isset($arrMaxDate) ? $arrMaxDate[0]['date'] : null;
					$intForecastDate	= '30';
					$intBufferDate		= '15';
					#$intSupplierTime	= $this->_request->getParam('intSupplierTime'.$i, null);
				
					$strSaleDateFrom1	= isset($arrMinDate) ? $arrMinDate[0]['date'] : null;
					$strSaleDateTo1		= isset($arrMaxDate) ? $arrMaxDate[0]['date'] : null;
					$intForecastDate1   = '30';
					$intBufferDate1		= '15';
				
					$arrCateData[]		= array
						(
							'name'	=> $strCategory,
							's_df'	=> $strSaleDateFrom,
							's_dt'	=> $strSaleDateTo,
							'fd'	=> $intForecastDate,
							'bd'	=> $intBufferDate,
							's_df1'	=> $strSaleDateFrom1,
							's_dt1'	=> $strSaleDateTo1,
							'fd1'	=> $intForecastDate1,
							'bd1'	=> $intBufferDate1
							);
				}
			
				$arrData				= array();
				$arrData['category']	= $arrCateData;
				$arrData['T']			= array(
					'w1'	=> '0.4',
					'w2'	=> '0.3',
					'w3'	=> '0.2',
					'w4'	=> '0.1'
					);
			
				$objCategory	= new CCategory($this->_country);
				$objCategory->save($arrData);
				CCommon::clear_chart_cache($this->_country);
		}
	}
}
?>

